import requests
import re
if __name__ == '__main__':
    from spider import Spider
else:
    from .spider import Spider


class FilmSearch:

    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3573.0 Safari/537.36'}

    ############# 特征 ##########
    def search(self, keyword):

        url = 'http://www.123ku.com/index.php?m=vod-search'

        r = requests.post(
            url,
            headers = self.headers,
            data={'wd': keyword, 'submit': 'search'},
        )

        r.encoding = 'utf-8'

        regex = '<li><span class="tt"></span><span class="xing_vb4"><a href="(.*?)" target="_blank">(.*?)</a></span> <span class="xing_vb5">(.*?)</span> <span class="xing_vb6">(.*?)</span></li>'

        #regex_img = '<img class="lazy" src="(.*?)"'

        info = re.findall(regex, r.text)

        #img_url = re.findall(regex_img, r.text)

        info = [('http://www.123ku.com'+ i[0], i[1], i[2], i[3]) for i in info]

        print(info)

        return {'info': info}

    def get_film_info(self, url):


        regex = dict(

        show_url = """name="copy_sel" value="(.*?)" checked=""><a title='(.*?)'""",
        
        img_url = '<img class="lazy" src="(.*?)" alt="(.*?)" />',

        bie_ming = '<!--别名开始-->(.*?)<!--别名结束-->',

        dao_yan = '<!--导演开始-->(.*?)<!--导演结束-->',

        zhu_yan = '<!--主演开始-->(.*?)<!--主演结束-->',

        lei_xing = '<!--类型开始-->(.*?)<!--类型结束-->',

        yu_yan = '<!--语言开始-->(.*?)<!--语言结束-->',

        shang_yin = '<!--上映开始-->(.*?)<!--上映结束-->',

        update = '<li>更新：<span>(.*?)</span></li>',

        jie_shao = '<!--介绍开始-->(.*?)<!--介绍结束-->',

        di_qu = "<!--地区开始-->(.*?)<!--地区结束-->",

        name = '<!--片名开始-->(.*?)<!--片名结束-->'


        )

        info = Spider().get_info(url, **regex)


        
        return info

    def get_film_info_brach(self, url):

        info = self.get_film_info(url)

        info['img_url'] = info['img_url'][0]

        #info['bie_ming'] = info['bie_ming'][0]

        info['dao_yan'] = re.findall("<a target='_blank' href='.*?'>(.*?)</a>", info['dao_yan'][0])

        info['zhu_yan'] = re.findall("<a target='_blank' href='.*?'>(.*?)</a>", info['zhu_yan'][0])

        info['update'] = info['update'][0]

        info['jie_shao'] = info['jie_shao'][0]
        info['lei_xing'] = info['lei_xing'][0]

        info['yu_yan'] = re.findall("<a target='_blank' href='.*?'>(.*?)</a>", info['yu_yan'][0])

        info['di_qu'] = re.findall("<a target='_blank' href='.*?'>(.*?)</a>", info['di_qu'][0])
        info['shang_yin'] = re.findall("<a target='_blank' href='.*?'>(.*?)</a>", info['shang_yin'][0])
        return info

    def get_search_info(self, keyword):

        search_info = self.search(keyword)['info'][:5]#获取数据[(url, 电影名称, 类别， 更新时间), ...]
       
        #info = [(i[0].replace('http://www.123ku.com/','/app/123ku/'), 
        #i[1], i[2], i[3]) for i in search_info['info']]#清洗数据

        search = []

        for i in search_info:

            info = self.get_film_info_brach(i[0])
            info['film_url'] = i[0].replace('http://www.123ku.com/','/app/123ku/')

            search.append(info)

        
        return search






        '''

        [{'show_url': [('https://www.697313.com:65/20190505/SizyjbbK/index.m3u8', 'HD高清'), ('https://www.697313.com:65/share/X6vI8VpQ1mU0xDQa', 'HD高清')], 'img_url': ('https://img.sokoyo-rj.com/tuku/upload/vod/2019-05-05/201905051557058802.jpg', '筑梦情缘发布会'), 'bie_ming': [''], 'dao_yan': ['金沙', '韩冬'], 'zhu_yan': ['霍建华', '杨幂', '曾江', '冯雷'], 'lei_xing': '综艺 ', 'yu_yan': ['国语'], 'shang_yin': ['2019'], 'update': '2019-05-05 20:12', 'jie_shao': '....', 'di_qu': ['大陆'], 'name': ['筑梦情缘发布会']}
        ,...]

        '''

        #print(search_info)


    ###### 功能区 #######
        

if __name__ == '__main__':

    url = 'http://www.123ku.com/?m=vod-detail-id-13379.html'

    x = FilmSearch()
    info = x.get_film_info_brach('http://www.123ku.com/?m=vod-detail-id-12051.html')
    #print(info)

    '''
    {'show_url': [('https://www.sokoyo-rj.com:65/20190507/bjN0wFDQ/index.m3u8', '第01集'), 
    ('https://www.sokoyo-rj.com:65/20190507/m5S3Shf8/index.m3u8', '第02集'), 
    ('https://www.sokoyo-rj.com:65/20190508/bswcPUBi/index.m3u8', '第03集'), 
    ('https://www.sokoyo-rj.com:65/20190508/2l59x6vm/index.m3u8', '第04集'), 
    ('https://www.sokoyo-rj.com:65/20190509/5nPc2tWq/index.m3u8', '第05集'), 
    ('https://www.sokoyo-rj.com:65/20190509/lDUtU21K/index.m3u8', '第06集'), 
    ('https://www.0518yaj.com:65/20190510/DcCBo7Ce/index.m3u8', '第07集'), 
    ('https://www.0518yaj.com:65/20190510/lsygc98q/index.m3u8', '第08集'), 
    ('https://www.sokoyo-rj.com:65/20190511/Sf6ibp8e/index.m3u8', '第09集'), 
    ('https://www.sokoyo-rj.com:65/20190512/Nj1wd8Xh/index.m3u8', '第10集'), 
    ('https://www.625135.com/20190513/0utBsGqr/index.m3u8', '第11集'), 
    ('https://www.625135.com/20190513/UZ6j7QDI/index.m3u8', '第12集'), 
    ('https://www.sokoyo-rj.com:65/share/MYYQe7vBnQ9MGbiA', '第01集'), 
    ('https://www.sokoyo-rj.com:65/share/ilZQAvStsy7YDc6Z', '第02集'), 
    ('https://www.sokoyo-rj.com:65/share/rovXmOBxHyBYoMfo', '第03集'), 
    ('https://www.sokoyo-rj.com:65/share/OvppGSNkkX2FZj92', '第04集'), 
    ('https://www.sokoyo-rj.com:65/share/xoJrYanU8SMlKROh', '第05集'), 
    ('https://www.sokoyo-rj.com:65/share/KPxM4bf9UGBbpmGY', '第06集'), 
    ('https://www.0518yaj.com:65/share/GSrmczSNlMWaF1yW', '第07集'), 
    ('https://www.0518yaj.com:65/share/MJfaiHEXUZxG6I7C', '第08集'), 
    ('https://www.sokoyo-rj.com:65/share/cUPgZ6Q2PE1cBk5r', '第09集'), 
    ('https://www.sokoyo-rj.com:65/share/9zn76EzwNn2htI', '第10集'), 
    ('https://www.625135.com/share/1pD3iZP27CuJLAUq', '第11集'), 
    ('https://www.625135.com/share/6m9zCoEDT15Rwlgd', '第12集')], 
    'img_url': [('https://img.sokoyo-rj.com/tuku/upload/vod/2019-05-07/201905071557161120.jpg', '筑梦情缘')], 
    'bie_ming': [''], 
    'dao_yan': ["<a target='_blank' href='/index.php?m=vod-search-directed-%E9%87%91%E6%B2%99'>金沙</a>&nbsp;<a target='_blank' href='/index.php?m=vod-search-directed-%E9%9F%A9%E5%86%AC'>韩冬</a>&nbsp;"], 
    'zhu_yan': ["<a target='_blank' href='/index.php?m=vod-search-starring-%E9%9C%8D%E5%BB%BA%E5%8D%8E'>霍建华</a>&nbsp;<a target='_blank' href='/index.php?m=vod-search-starring-%E6%9D%A8%E5%B9%82'>杨幂</a>&nbsp;<a target='_blank' href='/index.php?m=vod-search-starring-%E6%9B%BE%E6%B1%9F'>曾江</a>&nbsp;<a target='_blank' href='/index.php?m=vod-search-starring-%E5%86%AF%E9%9B%B7'>冯雷</a>&nbsp;<a target='_blank' href='/index.php?m=vod-search-starring-%E5%A7%9C%E5%AE%8F%E6%B3%A2'>姜宏波</a>&nbsp;<atarget='_blank' href='/index.php?m=vod-search-starring-%E5%BC%A0%E5%B3%BB%E5%AE%81'>张峻宁</a>&nbsp;<a target='_blank' href='/index.php?m=vod-search-starring-%E8%91%9B%E6%96%BD%E6%95%8F'>葛施敏</a>&nbsp;<a target='_blank' href='/index.php?m=vod-search-starring-%E7%AB%A0%E7%94%B3'>章申</a>&nbsp;<a target='_blank' href='/index.php?m=vod-search-starring-%E6%9B%B2%E9%AB%98%E4%BD%8D'>曲高位</a>&nbsp;<a target='_blank' href='/index.php?m=vod-search-starring-%E7%8E%8B%E9%AA%81'>王骁</a>&nbsp;"], 
    'lei_xing': ['国产剧 '], 
    'yu_yan': ["<a target='_blank' href='/index.php?m=vod-search-lang-%E5%9B%BD%E8%AF%AD'>国语</a>&nbsp;"], 
    'shang_yin': [], 
    'update': ['2019-05-07 00:34'], 
    'jie_shao': ['二十世纪二十年代初，乱世中少年沈其南一家忽遭飞来横祸。父母蒙难，兄妹四
人历经艰险逃到上海却不幸离散。在绝望中经历层层考验的沈其南与富家大小姐建筑设计师傅函君青梅竹马长大。长大后，
沈其南和傅函君怀揣着民族梦想，坚守“以人为本”的设计理念，将中....']}

    '''






        
